import { OrbitControls } from 'plugin/OrbitControls.js'

let hasGridHelper = true;
let hasAxesHelper = false;

// 预设的控制器
function PrecastOC(camera, element) {

    const controls = new OrbitControls(camera, element);
    // 启动阻尼，滑动更平滑
    controls.enableDamping = true;
    // 阻尼系数
    controls.dampingFactor = 0.1;
    // 启动缩放
    controls.enableZoom = true;
    controls.zoomSpeed = 1.0;
    // 启用滑动
    controls.enablePan = true;
    return controls;
}
// 预设的相机
function precastCamera() {
    // 创建相机
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(50, 50, 50);
    // 相机观察目标指向某个位置
    camera.lookAt(0, 0, 0); // 坐标原点
    return camera
}
// 预设的render
function precastRender() {
    // 获取容器
    const container = document.getElementById('towerCraneContainer');
    const renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(container.clientWidth, container.clientHeight);
    renderer.shadowMap.enabled = true;
    renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 使用软阴影
    renderer.outputEncoding = THREE.sRGBEncoding
    container.appendChild(renderer.domElement);
    return renderer
}

function mountLight(scene) {
    // // 添加环境光
    // const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
    // scene.add(ambientLight);
    // const pointLight = new THREE.PointLight(0xff0000, 0.8, 0, 1);
    // pointLight.position.set(0, 500, 0);
    // scene.add(pointLight)
    // const pointLight2 = new THREE.PointLight(0x00ff00, 0.8, 0, 1);
    // pointLight2.position.set(0, 500, 150);
    // scene.add(pointLight2)
    // const pointLight3 = new THREE.PointLight(0x0000ff, 0.8, 0, 1);
    // pointLight3.position.set(150, 500, 0);
    // scene.add(pointLight3)
    // const pointLightAll = new THREE.PointLight(0xffffff, 0.7);
    // pointLightAll.position.set(0, 400, 0);
    // pointLightAll.castShadow = true; // default false
    // //Set up shadow properties for the light
    // pointLightAll.shadow.mapSize.width = 512; // default
    // pointLightAll.shadow.mapSize.height = 512; // default
    // pointLightAll.shadow.camera.near = 0.5; // default
    // pointLightAll.shadow.camera.far = 1500 // default
    // scene.add(pointLightAll)
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
    scene.add(ambientLight);
}

function mountGround(scene) {
    // 添加地面
    // const groundGeometry = new THREE.PlaneGeometry(9400, 9400);
    // const groundMaterial = new THREE.MeshPhongMaterial({ color: 0xa0a0a0 });
    // const ground = new THREE.Mesh(groundGeometry, groundMaterial);
    // ground.rotation.x = -Math.PI / 2;
    // ground.position.x = 200
    // ground.position.y = 0;
    // ground.position.z = 200;
    // ground.receiveShadow = true;
    // scene.add(ground);
    const groundGeometry = new THREE.PlaneGeometry(300, 300);
    const groundMaterial = new THREE.MeshPhongMaterial({ color: 0xe0e0e0 });
    const ground = new THREE.Mesh(groundGeometry, groundMaterial);
    ground.rotation.x = -Math.PI / 2;
    ground.position.y = 0;
    ground.receiveShadow = true;
    scene.add(ground);
}

function moundGrid(scene) {

    if (hasGridHelper) {
        // 添加网格辅助线
        const gridHelper = new THREE.GridHelper(300, 20, 0xd0d0d0, 0xe0e0e0);
        scene.add(gridHelper);
    }
}

function mountAxes(scene) {
    if (hasAxesHelper) {
        // 设置坐标系
        const axesHelper = new THREE.AxesHelper(800)
        scene.add(axesHelper)

    }
}

export { PrecastOC, precastCamera, precastRender, mountLight, mountGround, moundGrid, mountAxes }